{extend name="common" /}

{block name='content'}
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-form">
            <input type="checkbox" lay-filter="monitor" lay-skin="switch" lay-text="实时监听开|实时监听关"/>
        </div>
        <table id="currentTable" class="layui-table layui-hide" lay-filter="currentTable"
               data-auth-add="1"
               data-auth-edit="1"
               data-auth-delete="1">
        </table>
    </div>
</div>
{/block}

{block name="js"}
<script>
    layui.extend({
        ea: 'lay-module/ea/ea.min'
    }).use(['ea', 'table', 'form'], function () {
        let ea = layui.ea,
            table = layui.table,
            form = layui.form,
            util = layui.util,
            intervalID,
            init = {
                table_elem: '#currentTable',
                table_render_id: 'currentTableRenderId',
                index_url: 'system_crontab/flow?sid=' + {$sid}
            };

        ea.table.render({
            init: init,
            toolbar: ['refresh'],
            cellMinWidth: 100,
            cols: [[
                {field: 'id', title: 'ID', sort: true, width: 80, search: false},
                {field: 'month', title: '日志月份', hide: true, search: 'time', timeType: 'month', searchValue: util.toDateString(new Date(), 'yyyy-MM')},
                {
                    field: 'return_var', title: '运行结果', selectList: {0: '成功', 1: '失败'}, templet: function (d) {
                        return d.return_var === 0 ? '<span class="layui-badge layui-bg-green">成功</span>' : '<span class="layui-badge layui-bg-red">失败</span>';
                    }
                },
                {field: 'command', title: '任务命令', search: false},
                {
                    field: 'running_time', title: '执行耗时', search: false, templet: function (d) {
                        return d.running_time + 's';
                    }
                },
                {
                    field: 'output', title: '执行输出', search: false, templet: function (d) {
                        return d.output.replace(/\n/g, "<br/>");
                    }
                },
                {field: 'create_time', title: '执行时间', sort: true, templet: ea.table.date, search: 'range'}
            ]],
        });

        form.on('switch(monitor)', function (data) {
            if (data.elem.checked) {
                intervalID = setInterval(function () {
                    table.reload(init.table_render_id);
                }, 1000);
            } else {
                clearInterval(intervalID);
            }

        });

        ea.listen();
    });
</script>
{/block}
